package com.ybu.oodd.service.impl;

import com.ybu.oodd.mapper.HomeMapper;
import com.ybu.oodd.pojo.HomeNum;
import com.ybu.oodd.pojo.HomeText;
import com.ybu.oodd.service.HomeService;
import com.ybu.oodd.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private RedisUtil redisUtil;

    /*
     * 查询统计数据 mysql
     * */
    @Override
    public HomeNum getHomeNumByRedis() {
        if (redisUtil.hasKey("homeNum")) {
            return new HomeNum()
                    .setTotalUsers(Integer.valueOf((String) redisUtil.hget("homeNum", "totalUsers")))
                    .setOnlineUsers(Integer.valueOf((String) redisUtil.hget("homeNum", "onlineUsers")))
                    .setCompanyCount(Integer.valueOf((String) redisUtil.hget("homeNum", "companyCount")))
                    .setJobCount(Integer.valueOf((String) redisUtil.hget("homeNum", "jobCount")))
                    .setDeliveredCount(Integer.valueOf((String) redisUtil.hget("homeNum", "deliveredCount")));
        }
        else return getHomeNumByMysql();
    }

    /*
     * 查询主页文字 redis
     * */
    @Override
    public HomeText getHomeTextByRedis() {
        String title = (String) redisUtil.get("homeTitle");
        String text = (String) redisUtil.get("homeText");
        if(title == null || title == "" || text == null || text == ""){
            return getHomeTextByMysql();
        }
        return new HomeText().setText(text).setTitle(title);
    }

    /*
     * 查询主页信息 mysql
     * */
    @Override
    public HomeText getHomeTextByMysql() {
        String text = homeMapper.getHomeText();
        String title = homeMapper.getHomeTitle();
        redisUtil.set("homeTitle",title,259200L);
        redisUtil.set("homeText",text,259200L);
        return new HomeText().setText(text).setTitle(title);
    }


    /*
     * 查询统计数据 mysql
     * */
    @Override
    public HomeNum getHomeNumByMysql() {
        return new HomeNum()
                //总人数
                .setTotalUsers(getTotalUsers())
                //在线人数
                .setOnlineUsers(getOnlineUsers())
                //公司数
                .setCompanyCount(getCompanyCount())
                //岗位数
                .setJobCount(getJobCount())
                //投递数
                .setDeliveredCount(getDeliveredCount());
    }

    /*
    * 投递数
    * */
    public Integer getDeliveredCount() {
        return homeMapper.getDeliveredCount();
    }

    /*
     * 岗位数
     * */
    public Integer getJobCount() {
        Integer jobCount = homeMapper.getJobCount();
        return jobCount==null?0:jobCount;
    }

    /*
    * 公司数
    * */
    public Integer getCompanyCount() {
        return homeMapper.getCompanyCount();
    }

    //在线人数
    public Integer getOnlineUsers() {
        Set<String> keys = redisUtil.scan("active-*",80);
        return keys.size();
    }

    //总人数
    public Integer getTotalUsers(){
        return homeMapper.getTotalUsers();
    }
}
